<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\Dashboard;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\InfoBox;
use Encore\Admin\Facades\Admin;
use App\Models\PayPipe;
use App\Models\PayLog;
use App\Models\RiskScoreConfig;
use Illuminate\Http\Request;
use GuzzleHttp\Client as GuzzleClient;

class AdminUserController extends Controller
{
    public function index(Content $content)
    {
        return Admin::content(function (Content $content) {

            $content->header('账号');
            $content->description('列表');

            $role = Admin::user()->roles->toArray();
            $allRole = [];
            foreach($role as $val){
                array_push($allRole, $val['slug']);
            }

            $show = 1;
            if(!in_array('administrator', $allRole)){
                $show = 2;
            }

            $homeView = view('admin.black',compact('show'))->render();
            $content->row($homeView);
            admin_toastr('提示','success');
        });
    }

    //获取所有的账号列表
    public function userlist(Request $request){
        $draw = $request->input('draw');//偏移量
        $length = $request->input('length');//偏移量
        $start = $request->input('start')/$length + 1;
        //获取搜索的关键词
        $keyword = $request->input('search.value');

        $params = [
            'headers' => [
                'ident' => md5(env('cacer_thor'))
            ],
        ];

        $get_url = env('thor_host').'api/controll/adminuser/list';

        $config_data = $this->guzzle($get_url, $params);
        $res = json_decode($config_data->getBody()->getContents(), true);
        $count = count($res);

        return response() -> json([
            'draw'              =>  $draw,//请求计数器
            'recordsTotal'      =>  $count,//数据表中总的记录数
            'recordsFiltered'   =>  $count,//被过滤之后的记录数
            'data'              =>  $res['data']
        ]);
    }

    //发送请求
    public function guzzle($url, $params)
    {
        $client = new GuzzleClient([
            // 超时
            'timeout' => 60.0,

        ]);
        return $client->request('GET', $url, $params);
    }

    public function guzzle_post($url, $params){
        $client = new GuzzleClient([
            // 超时
            'timeout' => 60.0,
        ]);
        return $client->request('POST', $url, $params);
    }

    //商户添加
    public function adduser(Request $request){
        $mehtod = $request->getMethod();
        if($mehtod == 'GET'){
            return view('admin.useradd');
        }else{
            $username = $request->input('username');
            $password = $request->input('password');
            $merchant = $request->input('merchant');

            if(!$username) return ['code' => 1, 'text' => '账号不能为空'];
            if(!$password) return ['code' => 1, 'text' => '密码不能为空'];

            $params = [
                'headers' => [
                    'ident' => md5(env('cacer_thor'))
                ],
                'json' => [
                    'username' => $username,
                    'password' => $password,
                    'merchant' => $merchant,
                ]
            ];

            $get_url = env('thor_host').'api/controll/adminuser/add';

            $config_data = $this->guzzle($get_url, $params);
            $res = json_decode($config_data->getBody()->getContents(), true);

            return $res;
        }
    }

    //商户编辑
    public function edituser(Request $request){
        $mehtod = $request->getMethod();
        if($mehtod == 'GET'){
            $tenant_id = $request->input('tenant_id');
            $params = [
                'headers' => [
                    'ident' => md5(env('cacer_thor'))
                ],
                'query' => [
                    'tenant_id' => $tenant_id,
                ]
            ];

            $get_url = env('thor_host').'api/controll/adminuser/edit';

            $config_data = $this->guzzle($get_url, $params);
            $res = json_decode($config_data->getBody()->getContents(), true);
            return view('admin.useredit', compact('res'));
        }else{
            $username = $request->input('username');
            $merchant = $request->input('merchant');
            $tenant_id = $request->input('tenant_id');
            $password = $request->input('password', '');

            if(!$username) return ['code' => 1, 'text' => '账号不能为空'];
            if(!$merchant) return ['code' => 1, 'text' => '商户名称不能为空'];

            $params = [
                'headers' => [
                    'ident' => md5(env('cacer_thor'))
                ],
                'form_params' => [
                    'username' => $username,
                    'tenant_id' => $tenant_id,
                    'merchant' => $merchant,
                    'password' => $password,
                ]
            ];

            $get_url = env('thor_host').'api/controll/adminuser/edit';

            $config_data = $this->guzzle_post($get_url, $params);
            $res = json_decode($config_data->getBody()->getContents(), true);

            return $res;
        }
    }

    //商户充值
    public function chargeuser(Request $request){
        $mehtod = $request->getMethod();
        if($mehtod == 'GET'){
            return view('admin.chargeuser');
        }else{
            $cash = $request->input('cash');
            $tenant_id = $request->input('tenant_id');

            if(!$cash) return ['code' => 1, 'text' => '参数不能为空'];
            if(!$tenant_id) return ['code' => 1, 'text' => '参数不能为空'];

            $params = [
                'headers' => [
                    'ident' => md5(env('cacer_thor'))
                ],
                'form_params' => [
                    'tenant_id' => $tenant_id,
                    'cash' => $cash,
                ]
            ];

            $get_url = env('thor_host').'api/controll/adminuser/chargeuser';

            $config_data = $this->guzzle_post($get_url, $params);
            $res = json_decode($config_data->getBody()->getContents(), true);
            return $res;
        }
    }

    //商户扣费配置
    public function deductfee(Request $request){
        $mehtod = $request->getMethod();
        if($mehtod == 'GET'){
            $tenant_id = $request->input('tenant_id');
            $params = [
                'headers' => [
                    'ident' => md5(env('cacer_thor'))
                ],
                'query' => [
                    'tenant_id' => $tenant_id,
                ]
            ];

            $get_url = env('thor_host').'api/controll/adminuser/deductfee';

            $config_data = $this->guzzle($get_url, $params);
            $res = json_decode($config_data->getBody()->getContents(), true);
            return view('admin.deductfee', compact('res'));
        }else{
            $message_fee = $request->input('message_fee');
            $buer_fee = $request->input('buer_fee');
            $tenant_id = $request->input('tenant_id');
            $idcard_fee = $request->input('idcard_fee');
            $alive_fee = $request->input('alive_fee');
            $carrier_fee = $request->input('carrier_fee');
            $zimiqizha_fee = $request->input('zimiqizha_fee');
            $zimiqizha_report_fee = $request->input('zimiqizha_report_fee');
            $custody_fee = $request->input('custody_fee');
            $zimiqizha_attention_fee = $request->input('zimiqizha_attention_fee');
            $tianlang = $request->input('tianlang');

            if(!$tenant_id) return ['code' => 1, 'text' => '参数不能为空'];
            if(!$message_fee) return ['code' => 1, 'text' => '参数不能为空'];
            if(!$buer_fee) return ['code' => 1, 'text' => '参数不能为空'];
            if(!$idcard_fee) return ['code' => 1, 'text' => '参数不能为空'];
            if(!$alive_fee) return ['code' => 1, 'text' => '参数不能为空'];
            if(!$carrier_fee) return ['code' => 1, 'text' => '参数不能为空'];
            if(!$zimiqizha_fee) return ['code' => 1, 'text' => '参数不能为空'];
            if(!$zimiqizha_report_fee) return ['code' => 1, 'text' => '参数不能为空'];
            if(!$custody_fee) return ['code' => 1, 'text' => '参数不能为空'];
            if(!$zimiqizha_attention_fee) return ['code' => 1, 'text' => '参数不能为空'];
            if(!$tianlang) return ['code' => 1, 'text' => '参数不能为空'];

            $params = [
                'headers' => [
                    'ident' => md5(env('cacer_thor'))
                ],
                'form_params' => [
                    'tenant_id' => $tenant_id,
                    'buer_fee' => $buer_fee,
                    'message_fee' => $message_fee,
                    'idcard_fee' => $idcard_fee,
                    'alive_fee' => $alive_fee,
                    'carrier_fee' => $carrier_fee,
                    'zimiqizha_fee' => $zimiqizha_fee,
                    'zimiqizha_report_fee' => $zimiqizha_report_fee,
                    'custody_fee' => $custody_fee,
                    'zimiqizha_attention_fee' => $zimiqizha_attention_fee,
                    'tianlang' => $tianlang,
                ]
            ];

            $get_url = env('thor_host').'api/controll/adminuser/deductfee';

            $config_data = $this->guzzle_post($get_url, $params);
            $res = json_decode($config_data->getBody()->getContents(), true);
            return $res;
        }
    }
}